<?php
/**
 * @title 433. 最小基因变化 - 剪枝穷列
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $start
     * @param String $end
     * @param String[] $bank
     * @return Integer
     */
    function minMutation($start, $end, $bank) {
        if($start == $end){
            return 0;
        } else {}

        /**
         * @since 2022-05-07 基因库
         */
        $bankArray = [];
        foreach ($bank as $b){
            $bankArray[$b] = 1;
        }

        /**
         * @since 2022-05-07 end需要在基因库里
         */
        if(!isset($bankArray[$end])){
            return -1;
        } else {}

        /**
         * @since 2022-05-07 已经存在的
         */
        $hashArray = [$start => 1];

        /**
         * @since 2022-05-07 广度搜索
         */
        $depth = 1;
        $nodes = [$start];
        while($nodes){
            $nexts = [];
            foreach ($nodes as $str){
                /**
                 * @since 2022-05-07 0-7逐位改变
                 */
                for($i=0; $i<=7; $i++){
                    $s = $str;
                    foreach (["A","C","G","T"] as $char){
                        $s[$i] = $char;
                        if($s == $end){
                            return $depth;
                        } elseif(isset($bankArray[$s]) AND !isset($hashArray[$s])) {
                            $hashArray[$s] = 1;
                            $nexts[] = $s;
                        } else {}
                    }
                }
            }

            /**
             * @since 2022-05-07 下次迭代
             */
            if(empty($nexts)){
                return -1;
            } else {
                $depth++;
                $nodes = $nexts;
                // var_dump($nexts);
            }
        }

        /**
         * @return
         */
        return $depth;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202205/2022.05.07.php
 */
$datas = [
    ["AACCGGTT", "AACCGGTA", ["AACCGGTA"],], // 1
    ["AACCGGTT", "AAACGGTA", ["AACCGGTA","AACCGCTA","AAACGGTA"],], // 2
    ["AAAAACCC", "AACCCCCC", ["AAAACCCC","AAACCCCC","AACCCCCC"],], // 3
    ["AAAAAACC", "AACCCCCC", ["AAAACCCC"],], // -1
    ["AACCGGTT", "AACCGGTA", []], // -1
    ["AACCGGTT", "AAACGGTA", ["AACCGATT","AACCGATA","AAACGATA","AAACGGTA"]], // 4
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->minMutation(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
